 <?php
/* 
	登录页
 */

 class Login extends WL_controller{

	public function __construct(){
		parent::__construct();
		//加载log模型
		$this->load->model('log_model');
	}
	
	/***********************************
		登录页
	 */
	public function index(){

		if( parent::is_logged() )
			redirect( '/index' );

		$this->load->view( 'login', $this->data);
	}
	
	
	/**
	 * 用户登录ajax-post提交验证
	 * @param uid
	 * @param password
	 *
	 */
	public function login_check(){

		$post = $this->input->post( null, true );
		$json = array(
					'status'=>'error',
					'error'=>'未知错误',
		);
		if( ! $this->data['site']->is_open && $post[ 'uid' ] != 'admin' ){
			$json[ 'error' ] = '系统关闭，禁止访问';
			echo json_encode( $json ); return FALSE;
		}
		if( empty( $post['uid'] ) ){
			$json['error'] = '用户名为空';
			echo json_encode( $json ); return FALSE;
		}

		if( empty( $post['password' ] ) ){
			$json['error'] = '密码为空';
			echo json_encode( $json ); return FALSE;
		}

		$this->load->model( 'user_model' );
		if( $user = $this->user_model->get_user_by_uid( $post['uid'] ) ){
			if( $user->password === md5( $post['password'] ) ){
				$json[ 'status' ] = 'success';
				$json[ 'error' ] = '登录成功';
				
				//写入session
				set_sess( 'id', $user->id );
				set_sess( 'uid', $user->uid );
				set_sess( 'name', $user->name );
				set_sess( 'email', $user->email );
								
				// 写入log
				$this->load->model( 'log_model' );
				$this->log_model->write_log( '登录系统' );

			}else{
				$json[ 'error' ] = '密码错误';
			}
		}else{
			$json[ 'error' ] = '用户不存在';
		}

		echo json_encode( $json ); return FALSE;
	}	


	/**
		注销用户
	 */
	
	public function logout(){
		
		// 写入log
		if( sess( 'id' ) ){
			$this->load->model( 'log_model' );
			$this->log_model->write_log( '退出系统' );
		}
		$this->session->sess_destroy();	//注销session
		redirect("/login");
	}

}

?>